//
//  VisitorView.swift
//  Weibo1.0
//
//  Created by 黄礼潮 on 16/2/16.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit

class VisitorView: UIView {
    
      
   /// MARK - 设置视图信息
    func setupInfo(imageName:String?,title:String){
        
        messageLabel.text = title
        
        guard let imgName = imageName else{
            
            startAnimation()
        
            return
        }
        
        iconView.image = UIImage(named:imgName)
        
        homeView.hidden = true
        
        maskIconView.hidden = true
        //方法2
        //sendSubviewToBack(maskIconView)  将遮罩视图挪到最低层
     
    }
    
      private func startAnimation(){
    
       let  anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        //用在不断重复的动画中，当动画绑定的视图销毁的时候，对应的动画也自动销毁
        anim.removedOnCompletion = false //这句话的作用，当跳转到别的界面再回来，动画还是转的！！！
      
        //将动画添加到图存
        iconView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    
   //initWitFrame 是UIView的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
   //initWithCoder 是用 SB 或 Xib 加载的函数
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")  //可以阻止用 SB 调用这个视图  会崩溃
         super.init(coder: aDecoder)
        setupUI()
    }
  // 懒加载 --控件
    
 /// 图标 ：image  构造函数创建 的 imageView 默认的就是image 的大小
    private lazy var iconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
/// 遮罩视图
    private lazy var maskIconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
// MARK: - 小房子
    private lazy var homeView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
// MARK: - 文字描述
    /// 只能提示时有个title 后面的没有  因为默认了
    private lazy var messageLabel:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")

    // MARK: - 注册按钮
     lazy var registerButon:UIButton = UIButton(title: "注册", imageName: "common_button_white_disable", color: UIColor.orangeColor())
    
// MARK: -  登录按钮
     lazy var loginButon:UIButton = UIButton(title: "登录", imageName: "common_button_white_disable", color: UIColor.darkGrayColor())
}


extension VisitorView {

    private func setupUI (){
    
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButon)
        addSubview(loginButon)
        
        //自动布局
        
        //不写下面的代码会崩溃   系统默认是true 通过setFrame来设置控件的位置
        //false 通过自动布局来设置控件的位置
       // iconView.translatesAutoresizingMaskIntoConstraints = false
        
        for v in subviews{
          v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        // ->小房子  统一选一个参照物
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // ->消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // ->注册按钮
        addConstraint(NSLayoutConstraint(item: registerButon, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButon, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButon, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButon, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //->登录按钮
        addConstraint(NSLayoutConstraint(item: loginButon, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButon, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButon, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButon, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // -> 遮罩视图
// 
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regBtn]", options: [], metrics: ["btnHeight":-36], views: ["mask":maskIconView,"regBtn":registerButon]))
        //设置背景颜色  使跟遮罩视图的颜色一样
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
               
    }



}